#include "widget.h"
#include "ui_widget.h"
#include <QFile>
#include <QSettings>
#include <QHBoxLayout>
#include <QPushButton>
#include <QDebug>
#include "protocol.h"
const QString INIFILENAME = ".\\Monitor.ini";
Widget::Widget(QWidget *parent) :
    QWidget(parent),m_map(this),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    getDefaultIp();
    addVehForm();
    connect(&m_msgSender, SIGNAL(connected()), this, SLOT(connected()));
    connect(&m_msgSender, SIGNAL(errorString(QString)), this, SLOT(showError(QString)));
    connect(&m_msgSender, SIGNAL(showVehInfo(VEHICLE*)), this, SLOT(displayVehQueue(VEHICLE*)));
    connect(&m_msgSender, SIGNAL(showHeadVeh(VEHICLE)), this, SLOT(displayFirstVeh(VEHICLE)));
    //设置goto...按钮为不可点击，只有在连接到服务端之后才可使用
    ui->pushButton_21->setEnabled(false);
    //使用signalmap将修改车型按钮的clicked信号重新映射
    QList<QPushButton *> allPButtons = ui->groupBox_2->findChildren< QPushButton* >();
    foreach(QPushButton* item, allPButtons)
    {
        connect(item, SIGNAL(clicked()), &m_map, SLOT(map()));
        m_map.setMapping(item, item->text());
    }
    connect(&m_map, SIGNAL(mapped(QString)), this, SLOT(vehClassButtonClicked(QString)));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::getDefaultIp()
{
    if(!QFile::exists(INIFILENAME))
    {
        QFile file(INIFILENAME);
        file.open(QIODevice::ReadOnly);
        file.close();
    }
    QSettings settings(INIFILENAME, QSettings::IniFormat);
    QString sIp = settings.value("/Host/IP").toString();
    ui->lineEdit->setText(sIp);
}

void Widget::on_pushButton_clicked()
{
    QString sIp = ui->lineEdit->text();
    if(sIp.isEmpty())
    {
        ui->label_2->setText("IP地址不能为空");
        return;
    }
    QSettings settings(INIFILENAME, QSettings::IniFormat);
    settings.setValue("/Host/IP", sIp);
    m_msgSender.connectToHost(sIp,PORT);
    ui->label_2->setText("连接中...");
}

void Widget::connected()
{
    ui->stackedWidget->setCurrentIndex(1);
    showError("连接服务端成功");
    ui->pushButton_21->setEnabled(true);
}

void Widget::showError(const QString& sError)
{
    ui->label_2->setText(sError);
}

void Widget::addVehForm()
{
    QLayout *layout = ui->stackedWidget->widget(1)->layout();
    for(int i = 0;i < MAX_VEHFORM;i++)
    {
        if(i == 0)
        {
            m_vehFormArray[i].setDesc("已发卡");
        }
        else
        {
            m_vehFormArray[i].setDesc("待发卡");
        }
        layout->addWidget(&m_vehFormArray[i]);
    }
}

void Widget::showResponse(RESTYPE resType)
{
    switch(resType)
    {
    case SUCCESS:
        showError("服务端回复成功报文，控制成功！");
        break;
    case FAIL:
        showError("服务端回复失败报文，控制失败，请再次尝试！");
        break;
    default:
        break;
    }
}

void Widget::displayVehQueue(VEHICLE *vehQueue)
{
    for(int i = 1; i < MAX_VEHFORM; i++)
    {
        m_vehFormArray[i].setAutoVehType(vehQueue[i -1]);
    }
    showError("从服务端获取到车型信息");
}

void Widget::displayFirstVeh(const VEHICLE &headVeh)
{
    m_vehFormArray[1].setAutoVehType(headVeh);
}

void Widget::on_pushButton_21_clicked()
{
    static bool flag = false;
    if(flag)
    {
        ui->stackedWidget->setCurrentIndex(1);
    }
    else
    {
        ui->stackedWidget->setCurrentIndex(2);
    }
    flag = !flag;
}

void Widget::on_pushButton_2_clicked()
{
    m_msgSender.modifyVehQueue(DELVEH, 0);
}

void Widget::on_pushButton_19_clicked()
{
    m_msgSender.modifyVehQueue(ADDVEH, 0);
}

void Widget::vehClassButtonClicked(const QString &sVehClass)
{
    m_msgSender.modifyVehQueue(MODVEH, sVehClass.toInt());
}
